<?php
class ProductAction extends Action {
	

	//管理供应信息
    public function sell(){
		
		$keys = explode(',',$seo_Arr);
		$SEO['title'] = '中国最新最全的建材求购,供应,招商,加盟信息平台-都市家建材网';
		$SEO['keywords'] = '建材,建材网,建材网站,建材新闻,建材供应,建材采购,建材招商,建材加盟,建材展会,建材企业,建材价格,建材销售,建材市场,建材产品,建材报价,建材品牌';
		$SEO['description'] = '专业提供建材产品信息,主要内容有:建材价格,建材报价,建材品牌,建材招商加盟及建材代理信息,建材商品的求购,供应,招商与合作信息;为广大建材行业的个人与企业同行们，提供海量的建材信息与商机，努力打造成国内建材行业信息免费发布平台.';
		$this->assign('SEO',$SEO);// SEO相关
		
		
		$uid = $_SESSION[C('USER_AUTH_KEY')];
		
		$Product   =   D('Product');
		
		import("ORG.Util.Page");// 导入Pinyin类
		$Page = new Page();

		$admin['p'] = $currentpage;$_SESSION['cm_jumpurl'] = U('product/sell',$admin);
		$p = $this->_get('p');
		$nowPage = isset($p)?$p:1;
		$where = "uid=$uid and type=1";
		$count      = $Product->where($where)->count();// 查询满足要求的总记录数
		$Page       = new Page($count,5);// 实例化分页类 传入总记录数
		$list = $Product->where($where)->order('id desc')->page($nowPage,$Page->listRows)->select();
		//特殊处理URL
		//$Page->url = U('/uid/'.$c.'/__PAGE__');
		$Page->kylingood = 1;
		$Page->rollPage = 5;
		$pageshow = $Page->show();// 分页显示输出

		$this->assign('pages',$pageshow);// 赋值分页输出
		$this->assign('list',$list);// 赋值数据集


		$this->display(); // 输出模板

    }
	

	
	//发布供应信息
	public function add(){

		if(IS_POST)
    	{
			$Product   =   D('Product');
			
			if($Product->create()) {
				$data = $this->_post();
				$data['updatetime'] = $data['inputtime'] = time();
				$data['uid'] = $_SESSION[C('USER_AUTH_KEY')];
				$data['type'] = 1;
				$data['thumb']  = str_replace('/tmp/','/product/',$data['photofront']);

				
				if(empty($data['photofront'])){
					$filepath = str_replace('/tmp/','/product/',$data['pic'][0]);
					$data['thumb'] = $filepath;
				}
				
				
				$data['ProductData']['content'] = $data['content'];
				$data['ProductData']['brands'] = $data['brands'];
				$data['ProductData']['model'] = $data['model'];
				$data['ProductData']['standard'] = $data['standard'];

				$pic = $_POST['pic'];
				if (is_array($pic) && count($pic)) {
					Load('extend');
					foreach($pic as $keys => $val){
						$filepath = str_replace('/tmp/','/product/',$val);
						$target = C('UPLOAD_DIR').$filepath;
						$source = C('UPLOAD_DIR').$val;

						if (!is_dir($target)) createDir(dirname($target));
						
						@rename($source,$target);
						$data['Attachment'][$keys]['catid'] = $_POST['catid'];
						$data['Attachment'][$keys]['filename'] = basename($target);
						$data['Attachment'][$keys]['filepath'] = $filepath;
						$data['Attachment'][$keys]['fileext'] = strtolower(pathinfo($target, PATHINFO_EXTENSION));
						$data['Attachment'][$keys]['filesize'] = filesize($target);
						$data['Attachment'][$keys]['isimage'] = 1;
						$data['Attachment'][$keys]['userid'] = $_SESSION[C('USER_AUTH_KEY')];
						$data['Attachment'][$keys]['uploadtime'] = time();
						$data['Attachment'][$keys]['uploadip'] = ip();
						$data['Attachment'][$keys]['status'] = 1;
						$data['Attachment'][$keys]['siteid'] = 1;
					}
				}

				unset($data['content']);
				unset($data['brands']);
				unset($data['model']);
				unset($data['standard']);
				unset($data['nowpicnums']);
				unset($data['btnSubmit']);
				unset($data['photofront']);

				
				//关联插入
				$id = $Product->relation(true)->add($data);
				$d = array();
				
				if($id) {
					$d = array('id'=>$id);
					$this->ajaxReturn($d,'恭喜您，产品信息发布成功！',1);
				}else{
					$this->ajaxReturn($d,'对不起，产品信息发布失败！',0);
				}
			}else{

				//$this->error($Form->getError());
				$data['inputName'] = key($Product->getError());
				$info = current($Product->getError());
				$this->ajaxReturn($data,$info,0);
			}

		}else{
			
			
			$Category =   M('Category');
			$condition['parentid'] = 6;
			$CateArr = $Category->field('catname,catid')->where($condition)->select(); 
			$Categoryhtml = '<option value="">请选分类</option>';
			foreach($CateArr as $Cate) {
				$Categoryhtml.= '<option value="'.$Cate['catid'].'">'.$Cate['catname'].'</option>';
			}

			$Area =   M('Area');
			$condition['parentid'] = 1;
			$AreaArr = $Area->field('name,id')->where($condition)->select(); 
			$Areahtml = '<option value="">请选省市</option>';
			foreach($AreaArr as $Cate) {
				$Areahtml.= '<option value="'.$Cate['id'].'">'.$Cate['name'].'</option>';
			}
			$this->assign('Areahtml',$Areahtml);
			$this->assign('Categoryhtml',$Categoryhtml);
			$this->display();
		}
	}



	//修改供应信息
	public function edit(){

		$id = $this->_param('id');
		$Product   =   D('Product');
		if(IS_POST){
			
			
			$data = $this->_post();

			
			
			if(empty($data['thumb']) && $data['picimg'][0]){
				$data['thumb'] = $data['picimg'][0];
			}
			
			$filepath = str_replace('/tmp/','/product/',$data['pic'][0]);
			if(empty($data['thumb']) && empty($data['picimg'][0]) && $filepath){
				$data['thumb'] = $filepath;
			}
			

			$data['ProductData']['content'] = $data['content'];
			$data['ProductData']['brands'] = $data['brands'];
			$data['ProductData']['model'] = $data['model'];
			$data['ProductData']['standard'] = $data['standard'];
			unset($data['content']);
			unset($data['brands']);
			unset($data['model']);
			unset($data['standard']);
			unset($data['nowpicnums']);
			unset($data['btnSubmit']);

			//print_r($data);
			//关联更新
			$result = $Product->relation(true)->where("id=$id")->save($data);
			
			
			//处理新增加的图片
			$pic = $data['pic'];
			
			if (($result!== false) && is_array($pic)) {
				$Attachment = M('Attachment');

				Load('extend');
				foreach($pic as $val){
					$filepath = str_replace('/tmp/','/product/',$val);
					$target = C('UPLOAD_DIR').$filepath;
					$source = C('UPLOAD_DIR').$val;

					if (!is_dir($target)) createDir(dirname($target));
					
					@rename($source,$target);
					$attachment['id'] = $id;
					$attachment['catid'] = $_POST['catid'];
					$attachment['filename'] = basename($target);
					$attachment['filepath'] = $filepath;
					$attachment['fileext'] = strtolower(pathinfo($target, PATHINFO_EXTENSION));
					$attachment['filesize'] = filesize($target);
					$attachment['isimage'] = 1;
					$attachment['userid'] = $_SESSION[C('USER_AUTH_KEY')];
					$attachment['uploadtime'] = time();
					$attachment['uploadip'] = ip();
					$attachment['status'] = 1;
					$attachment['siteid'] = 1;
					$r = $Attachment->add($attachment);
					
				}
			}

			
			

			$d = array();
			if($result === false){
				$this->ajaxReturn($d,'对不起，产品信息修改失败！',0);
			}else{
				$d = array('id'=>$id);
				
				$this->ajaxReturn($d,'恭喜您，产品信息修改成功！',1);
				
			}
		
		}else{
			
			$data =    $Product->relation(true)->find($id);
			//print_r($data);exit;
			if($data) {

				$this->data =   $data;// 模板变量赋值
				$this->attachment =   $data['Attachment'];// 模板变量赋值
				$this->thumb =  $data['thumb'];
				//取到大分类
				$Category =   M('Category');
				$condition['parentid'] = 6;
				$CateArr = $Category->field('catname,catid')->where($condition)->select(); 
				$Categoryhtml = '<option value="">请选分类</option>';
				foreach($CateArr as $Cate) {
					$selected = '';
					if($Cate['catid']==$data['catid']) $selected = 'selected="selected"';
					$Categoryhtml.= '<option value="'.$Cate['catid'].'" '.$selected.' >'.$Cate['catname'].'</option>';
				}

				//取到小分类
				$condition['parentid'] = $data['catid'];
				$SonCateArr = $Category->field('catname,catid')->where($condition)->select(); 
				$SonCategoryhtml = '<option value="">请选子分类</option>';
				foreach($SonCateArr as $SonCate) {
					$selected = '';
					if($SonCate['catid']==$data['typeid']) $selected = 'selected="selected"';
					$SonCategoryhtml.= '<option value="'.$SonCate['catid'].'" '.$selected.' >'.$SonCate['catname'].'</option>';
				}



				//取到省会
				$Area =   M('Area');
				$condition['parentid'] = 1;
				$AreaArr = $Area->field('name,id')->where($condition)->select(); 
				$Areahtml = '<option value="">请选省市</option>';
				foreach($AreaArr as $Arealist) {
					$selected = '';
					if($Arealist['id']==$data['province'])  $selected = 'selected="selected"';
					$Areahtml.= '<option value="'.$Arealist['id'].'" '.$selected.' >'.$Arealist['name'].'</option>';
				}
				
				//取到市级
				$condition['parentid'] = $data['province'];
				$SonAreaArr = $Area->field('name,id')->where($condition)->select(); 
				$SonAreahtml = '<option value="">请选县区</option>';
				foreach($SonAreaArr as $SonArea) {
					$selected = '';
					if($SonArea['id']==$data['city']) $selected = 'selected="selected"';
					$SonAreahtml.= '<option value="'.$SonArea['id'].'" '.$selected.'>'.$SonArea['name'].'</option>';
				}
				

				$this->assign('Categoryhtml',$Categoryhtml);
				$this->assign('SonCategoryhtml',$SonCategoryhtml);
				$this->assign('Areahtml',$Areahtml);
				$this->assign('SonAreahtml',$SonAreahtml);
				

			}else{
			
				echo 'error';
			}
		}
		$this->display(); 
	}

	


	//管理采购信息
	public function buy(){
		
		$keys = explode(',',$seo_Arr);
		$SEO['title'] = '中国最新最全的建材求购,供应,招商,加盟信息平台-都市家建材网';
		$SEO['keywords'] = '建材,建材网,建材网站,建材新闻,建材供应,建材采购,建材招商,建材加盟,建材展会,建材企业,建材价格,建材销售,建材市场,建材产品,建材报价,建材品牌';
		$SEO['description'] = '专业提供建材产品信息,主要内容有:建材价格,建材报价,建材品牌,建材招商加盟及建材代理信息,建材商品的求购,供应,招商与合作信息;为广大建材行业的个人与企业同行们，提供海量的建材信息与商机，努力打造成国内建材行业信息免费发布平台.';
		$this->assign('SEO',$SEO);// SEO相关
		
		
		$uid = $_SESSION[C('USER_AUTH_KEY')];
		
		$Product   =   D('Product');
		
		import("ORG.Util.Page");// 导入Pinyin类
		$Page = new Page();

		$admin['p'] = $currentpage;$_SESSION['cm_jumpurl'] = U('product/buy',$admin);
		$p = $this->_get('p');
		$nowPage = isset($p)?$p:1;
		$where = "uid=$uid  and type=2";
		$count      = $Product->where($where)->count();// 查询满足要求的总记录数
		$Page       = new Page($count,5);// 实例化分页类 传入总记录数
		$list = $Product->where($where)->order('id desc')->page($nowPage,$Page->listRows)->select();
		//特殊处理URL
		//$Page->url = U('/uid/'.$c.'/__PAGE__');
		$Page->kylingood = 1;
		$Page->rollPage = 5;
		$pageshow = $Page->show();// 分页显示输出

		$this->assign('pages',$pageshow);// 赋值分页输出
		$this->assign('list',$list);// 赋值数据集


		$this->display(); // 输出模板

    }
	

	//发布采购信息
	public function addbuy(){
		
		if(IS_POST)
    	{
			$Product   =   D('Product');
			
			if($Product->create()) {
				$data = $this->_post();

				

				$data['updatetime'] = $data['inputtime'] = time();
				$data['uid'] = $_SESSION[C('USER_AUTH_KEY')];
				$data['type'] = 2;
				
				$data['thumb']  = str_replace('/tmp/','/product/',$data['photofront']);

				
				if(empty($data['photofront'])){
					$filepath = str_replace('/tmp/','/product/',$data['pic'][0]);
					$data['thumb'] = $filepath;
				}
				
				
				$data['ProductData']['content'] = $data['content'];
				$data['ProductData']['address'] = $data['address'];
				$data['ProductData']['contact'] = $data['contact'];

				$pic = $_POST['pic'];
				if (is_array($pic) && count($pic)) {
					Load('extend');
					foreach($pic as $keys => $val){
						$filepath = str_replace('/tmp/','/product/',$val);
						$target = C('UPLOAD_DIR').$filepath;
						$source = C('UPLOAD_DIR').$val;

						if (!is_dir($target)) createDir(dirname($target));
						
						@rename($source,$target);
						$data['Attachment'][$keys]['catid'] = $_POST['catid'];
						$data['Attachment'][$keys]['filename'] = basename($target);
						$data['Attachment'][$keys]['filepath'] = $filepath;
						$data['Attachment'][$keys]['fileext'] = strtolower(pathinfo($target, PATHINFO_EXTENSION));
						$data['Attachment'][$keys]['filesize'] = filesize($target);
						$data['Attachment'][$keys]['isimage'] = 1;
						$data['Attachment'][$keys]['userid'] = $_SESSION[C('USER_AUTH_KEY')];
						$data['Attachment'][$keys]['uploadtime'] = time();
						$data['Attachment'][$keys]['uploadip'] = ip();
						$data['Attachment'][$keys]['status'] = 1;
						$data['Attachment'][$keys]['siteid'] = 1;
					}
				}
				
				unset($data['content']);
				unset($data['address']);
				unset($data['brands']);
				unset($data['contact']);
				unset($data['model']);
				unset($data['standard']);
				unset($data['nowpicnums']);
				unset($data['btnSubmit']);
				unset($data['photofront']);

				
				//关联插入
				$id = $Product->relation(true)->add($data);
				$d = array();
				
				if($id) {
					$d = array('id'=>$id);
					$this->ajaxReturn($d,'恭喜您，采购信息发布成功！',1);
				}else{
					$this->ajaxReturn($d,'对不起，采购信息发布失败！',0);
				}
			}else{

				//$this->error($Form->getError());
				$data['inputName'] = key($Product->getError());
				$info = current($Product->getError());
				$this->ajaxReturn($data,$info,0);
			}

		}else{
			
			
			$Category =   M('Category');
			$condition['parentid'] = 6;
			$CateArr = $Category->field('catname,catid')->where($condition)->select(); 
			$Categoryhtml = '<option value="">请选分类</option>';
			foreach($CateArr as $Cate) {
				$Categoryhtml.= '<option value="'.$Cate['catid'].'">'.$Cate['catname'].'</option>';
			}

			$Area =   M('Area');
			$condition['parentid'] = 1;
			$AreaArr = $Area->field('name,id')->where($condition)->select(); 
			$Areahtml = '<option value="">请选省市</option>';
			foreach($AreaArr as $Cate) {
				$Areahtml.= '<option value="'.$Cate['id'].'">'.$Cate['name'].'</option>';
			}
			$this->assign('Areahtml',$Areahtml);
			$this->assign('Categoryhtml',$Categoryhtml);
			$this->display();
		}
	}
	

	//修改求购信息
	public function editbuy(){

		$id = $this->_param('id');
		$Product   =   D('Product');
		if(IS_POST){
			
			
			$data = $this->_post();

			if(empty($data['thumb']) && $data['picimg'][0]){
				$data['thumb'] = $data['picimg'][0];
			}
			
			$filepath = str_replace('/tmp/','/product/',$data['pic'][0]);
			if(empty($data['thumb']) && empty($data['picimg'][0]) && $filepath){
				$data['thumb'] = $filepath;
			}
			

			$data['ProductData']['content'] = $data['content'];
			$data['ProductData']['address'] = $data['address'];
			$data['ProductData']['contact'] = $data['contact'];


			unset($data['content']);
			unset($data['address']);
			unset($data['contact']);
			unset($data['model']);
			unset($data['brands']);
			unset($data['model']);
			unset($data['standard']);
			unset($data['nowpicnums']);
			unset($data['btnSubmit']);

			//print_r($data);
			//关联更新
			$result = $Product->relation(true)->where("id=$id")->save($data);
			
			
			//处理新增加的图片
			$pic = $data['pic'];
			
			if (($result!== false) && is_array($pic)) {
				$Attachment = M('Attachment');

				Load('extend');
				foreach($pic as $val){
					$filepath = str_replace('/tmp/','/product/',$val);
					$target = C('UPLOAD_DIR').$filepath;
					$source = C('UPLOAD_DIR').$val;

					if (!is_dir($target)) createDir(dirname($target));
					
					@rename($source,$target);
					$attachment['id'] = $id;
					$attachment['catid'] = $_POST['catid'];
					$attachment['filename'] = basename($target);
					$attachment['filepath'] = $filepath;
					$attachment['fileext'] = strtolower(pathinfo($target, PATHINFO_EXTENSION));
					$attachment['filesize'] = filesize($target);
					$attachment['isimage'] = 1;
					$attachment['userid'] = $_SESSION[C('USER_AUTH_KEY')];
					$attachment['uploadtime'] = time();
					$attachment['uploadip'] = ip();
					$attachment['status'] = 1;
					$attachment['siteid'] = 1;
					$r = $Attachment->add($attachment);
					
				}
			}

			
			

			$d = array();
			if($result === false){
				$this->ajaxReturn($d,'对不起，产品信息修改失败！',0);
			}else{
				$d = array('id'=>$id);
				
				$this->ajaxReturn($d,'恭喜您，产品信息修改成功！',1);
				
			}
		
		}else{
			
			$data =    $Product->relation(true)->find($id);
			//print_r($data);exit;
			if($data) {

				$this->data =   $data;// 模板变量赋值
				$this->attachment =   $data['Attachment'];// 模板变量赋值
				$this->thumb =  $data['thumb'];
				//取到大分类
				$Category =   M('Category');
				$condition['parentid'] = 6;
				$CateArr = $Category->field('catname,catid')->where($condition)->select(); 
				$Categoryhtml = '<option value="">请选分类</option>';
				foreach($CateArr as $Cate) {
					$selected = '';
					if($Cate['catid']==$data['catid']) $selected = 'selected="selected"';
					$Categoryhtml.= '<option value="'.$Cate['catid'].'" '.$selected.' >'.$Cate['catname'].'</option>';
				}

				//取到小分类
				$condition['parentid'] = $data['catid'];
				$SonCateArr = $Category->field('catname,catid')->where($condition)->select(); 
				$SonCategoryhtml = '<option value="">请选子分类</option>';
				foreach($SonCateArr as $SonCate) {
					$selected = '';
					if($SonCate['catid']==$data['typeid']) $selected = 'selected="selected"';
					$SonCategoryhtml.= '<option value="'.$SonCate['catid'].'" '.$selected.' >'.$SonCate['catname'].'</option>';
				}



				//取到省会
				$Area =   M('Area');
				$condition['parentid'] = 1;
				$AreaArr = $Area->field('name,id')->where($condition)->select(); 
				$Areahtml = '<option value="">请选省市</option>';
				foreach($AreaArr as $Arealist) {
					$selected = '';
					if($Arealist['id']==$data['province'])  $selected = 'selected="selected"';
					$Areahtml.= '<option value="'.$Arealist['id'].'" '.$selected.' >'.$Arealist['name'].'</option>';
				}
				
				//取到市级
				$condition['parentid'] = $data['province'];
				$SonAreaArr = $Area->field('name,id')->where($condition)->select(); 
				$SonAreahtml = '<option value="">请选县区</option>';
				foreach($SonAreaArr as $SonArea) {
					$selected = '';
					if($SonArea['id']==$data['city']) $selected = 'selected="selected"';
					$SonAreahtml.= '<option value="'.$SonArea['id'].'" '.$selected.'>'.$SonArea['name'].'</option>';
				}
				

				$this->assign('Categoryhtml',$Categoryhtml);
				$this->assign('SonCategoryhtml',$SonCategoryhtml);
				$this->assign('Areahtml',$Areahtml);
				$this->assign('SonAreahtml',$SonAreahtml);
				

			}else{
			
				echo 'error';
			}
		}
		$this->display(); 
	}
	



	//操作信息
	public function doit(){
		if(IS_POST)
    	{
			$Product   =   D('Product');
			$id = $this->_param('id');
			$uid = $_SESSION[C('USER_AUTH_KEY')];
			$data['updatetime'] = time();
			$result = $Product->relation(true)->where("id=$id AND uid=$uid")->save($data);
			if($result === false){
				$this->ajaxReturn($data,'对不起，操作出错。请稍后重试。',0);
			}else{
				$this->ajaxReturn($data,'恭喜您，操作成功！',1);
			}
		}
		exit;
	}

	
	

	//招商信息
	public function business(){
	
		$this->display();
	}

	//管理招商，加盟信息
	public function manage(){
	
		$this->display(); 
	}


	//加盟信息
	public function join(){
	
		$this->display(); 
	}
	

	//删除信息
	public function delimg(){
		if(IS_POST)
    	{
			$Attachment = M('Attachment');
			$aid = $this->_param('id');
			$userid = $_SESSION[C('USER_AUTH_KEY')];
			$where = "userid = $userid AND aid=$aid";
			$result = $Attachment->where($where)->delete();

			$data = array();
			if($result === false){
				$this->ajaxReturn($data,'对不起，删除图片出错。请稍后重试。',0);
			}else{
				$this->ajaxReturn($data,'恭喜您，删除图片成功！',1);
			}
		}
		
	}

	//删除信息
	public function del(){
		if(IS_POST)
    	{
			$Product   =   D('Product');
			$id = $this->_param('id');
			$uid = $_SESSION[C('USER_AUTH_KEY')];;
			$result = $Product->relation(true)->where("uid=$uid")->delete($id);
			if($result === false){
				$this->ajaxReturn($data,'对不起，删除出错。请稍后重试。',0);
			}else{
				$this->ajaxReturn($data,'恭喜您，删除成功！',1);
			}
		}
		exit;
	}
	
	

}